<template>
    <el-card>
       <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="110px">
            <el-form-item label="前端服务名：" prop="service">
                <el-select v-model="queryParams.service" placeholder="请选择" size="small">
                    <el-option v-for="item in FrontendList" :label="item" :key="item" :value="item"></el-option>
                  </el-select>
            </el-form-item>
            <el-form-item label="前端API路径：" prop="path">
                <el-input v-model="queryParams.path" placeholder="前端API路径" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                </div>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="前端服务名" prop="service"></el-table-column>
            <el-table-column label="前端API路径" prop="path"></el-table-column>
            <el-table-column label="单机QPS" prop="qps"></el-table-column>
            <el-table-column label="单机并发数" prop="concurrent"></el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
          </el-table>
          <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
          </el-pagination>
           <el-dialog :title="title" :visible.sync="formVisible" width="40%">
            <el-form :model="addForm" :rules="rules" ref="addForm">
                <el-row :gutter="20">
                  <el-col :span="10">
                      <el-form-item  prop="service" label="前端服务名：" required :label-width="formLabelWidth">
                         <el-select v-model="addForm.service" placeholder="请选择" size="small">
                            <el-option v-for="item in FrontendList" :label="item" :key="item" :value="item"></el-option>
                         </el-select>
                      </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="path" label="前端API路径：" :label-width="formLabelWidth">
                          <el-input v-model="addForm.path" autocomplete="off" size="mini" placeholder="请输入前端API路径"></el-input>
                          <span style="font-size: 10px;opacity: 0.7">如果接口URL为http://网关IP:port/proxy/user-service/user/list, 则输入/user/list，如果不配置路径则按服务限流</span>
                      </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="20">
                    <el-form-item  prop="qps" label="集群QPS：" :label-width="formLabelWidth">
                        <div class="wrap">
                             <div class="tips">{{addForm.qps}}</div>
                             <el-col :span="1">=</el-col>
                             <el-col :span="6">
                                 <el-input v-model="addForm.qps" autocomplete="off"></el-input>
                             </el-col>
                             <el-col :span="4">单机*1</el-col>
                        </div>
                    </el-form-item>
                </el-row>
                <el-row :gutter="20">
                    <el-form-item  prop="concurrent" label="集群并发数：" :label-width="formLabelWidth">
                        <div class="wrap">
                             <div class="tips">{{addForm.concurrent}}</div>
                             <el-col :span="1">=</el-col>
                             <el-col :span="6">
                                 <el-input v-model="addForm.concurrent" autocomplete="off"></el-input>
                             </el-col>
                             <el-col :span="4">单机*1</el-col>
                        </div>
                    </el-form-item>
                </el-row>
                 <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="responseContentType" label="限流响应ContentType:" :label-width="formLabelWidth">
                          <el-input v-model="addForm.responseContentType" autocomplete="off" size="mini" placeholder="请输入ContentType"></el-input>
                          <span style="font-size: 10px;opacity: 0.7">如果不配置则使用默认规则里的限流响应ContentType</span>
                      </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="responseContent" label="限流响应报文:" :label-width="formLabelWidth">
                          <el-input type="textarea" :rows="4" v-model="addForm.responseContent" autocomplete="off" size="mini" placeholder="请输入响应报文"></el-input>
                          <span style="font-size: 10px;opacity: 0.7">如果不配置则使用默认规则里的限流响应报文</span>
                      </el-form-item>
                  </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSubmit('addForm')" size="small">保存</el-button>
                <el-button @click="clientFormVisible = false" size="small">取消</el-button>
            </div>
         </el-dialog>
    </el-card>
</template>
<script>
import {getApiFlowList,getFrontendDict,saveApiForm,remove,update} from '@/api/flow/apiFlow.api.js'
    export default {
        data() {
            return {
                queryParams: {
                    current: 1,
                    size: 10,
                    path: undefined,
                    service: undefined
                },
                total: 0,
                // 前端服务名
                FrontendList: [],
                formLabelWidth: '160px',
                tableData: [],
                title: '新增',
                responseContent: [],
                formVisible: false,
                rules: {
                    service: [{ required: true, message: '请选择前端服务名', trigger: 'blur' }],
                    qps: [{ pattern:/^[0-9]*[1-9][0-9]*$/,message: '请输入大于0的整数'}],
                    concurrent: [{ pattern:/^[0-9]*[1-9][0-9]*$/,message: '请输入大于0的整数'}],
                    path: [{ pattern: /(?=\/)/,message: "请参照例子正确填写路径, 开头不可缺少 '/' 符号" }]
                },
                addForm: {
                    service: undefined,
                    path: '',
                    qps: '',
                    concurrent: '',
                    responseContentType: '',
                    responseContent: ''
                }
            }
        },
        mounted() {
          this.getFrontendList();
          this.getList();
        },
        methods: {
            async getFrontendList() {
                const result = await getFrontendDict();
                 if(result.code === 200) {
                    this.FrontendList = result.data;
                }
            },
            getList() {
                getApiFlowList(this.queryParams).then(res=>{
                    if(res.code === 200) {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
            },
            // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
           },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            handleAdd() {
                this.addForm = {
                    service: '',
                    path: '',
                    qps: '',
                    concurrent: '',
                    responseContentType: '',
                    responseContent: ''
                }
                this.$refs["addForm"] && this.$refs["addForm"].resetFields();
                this.formVisible = true;
            },
            // 编辑
            handleEdit(row) {
                this.addForm = JSON.parse(JSON.stringify(row))
                this.title = '编辑';
                this.formVisible = true;
            },
            // 删除
            handleDelete(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await remove(id);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                           this.getList();
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
             handleSubmit(formName) {
               this.$refs[formName].validate(async (valid) => {
                 if (valid) {
                    if(this.addForm.id) {
                        const result = await update(this.addForm);
                         if(result.code===200) {
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                         }
                    }else{
                         const result = await saveApiForm(this.addForm);
                         if(result.code===200) {
                            this.$message({
                                message: '保存成功',
                                type: 'success'
                            });
                        }
                    }
                        this.formVisible = false;
                        this.getList();
                 } else {
                    console.log('error submit!!');
                    return false;
                 }
                });
            },
        }
    }
</script>
<style lang="scss" scoped>
.wrap {
    display: flex;
    flex-direction: row;
    .tips {
        display: inline-block;
        text-align: center;
        color: #67c23a;
        background: #f0f9eb;
        border: 1px solid #c2e7b0;
        border-radius: 3px;
        min-width: 80px;
        height: 36px;
        line-height: 30px;
        padding: 0 6px;
    }
}
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>